package com.kehao.newcoder;

public class NC24Solution {
    private class ListNode {
        int val;
        ListNode next = null;
        ListNode(int  val){
            this.val = val;
        }
    }
    public ListNode deleteDuplicates (ListNode head) {
        if(head==null||head.next == null) return head;
        ListNode h = new ListNode(0);
        h.next = head;
        ListNode p0 = h;
        ListNode p1 = head;
        ListNode p2 = head.next;
        while (p0!=null){
            if(p1==null||p2==null) {
                break;
            }
            if(p1.val ==p2.val){
                while (p2!=null&&p2.val== p1.val){
                    p2 = p2.next;
                }
                p0.next = p2;
                p1 = p2;
                if(p2!=null){
                    p2=p2.next;
                }

            }else {
                p0 = p0.next;
                p1 = p1.next;
                p2 = p2.next;
            }

        }
        return h.next;
    }
}
